#include "Player.h"
#include "InputHandler.h"

Player::Player():
    SDLGameObject()
{}

void Player::load(const LoaderParams * pParams){
    SDLGameObject::load(pParams);
}

void Player::draw(){
    SDLGameObject::draw();
}

void Player::update(){
    if (TheGame::Instance()->getLevelComplete())
    {
        if (m_position.getX() > TheGame::Instance()->getGameWidth())
        {
            TheGame::Instance()->setCurrentLevel(TheGame::Instance()
                    ->getCurrentLevel() + 1);
        }
        else
        {
            m_velocity.setY(0);
            m_velocity.setX(3);
            ShooterObject::update();
            handleAnimation();
        }
    }
    else
    {
       if (!m_bDying){
            m_velocity.setX(0);
            m_velocity.setY(0);

            handlerInput();
            ShooterObject::update();
            handleAnimation();
       }       
       else 
       {
            m_currentFrame = int(((SDL_GetTicks()/(100))) % m_numFrames);
            if (m_dyingCounter == m_dyingTime)
            {
                ressurect();
            }
            m_dyingCounter++;
       }
    }

#if 0    
//    m_x -= 1;
    m_currentFrame = int((SDL_GetTicks() /100) % 6);
   // m_acceleration.setX(1);
//    m_velocity.setX(1);
//
#if 0
    if (TheInputHandler::Instance()->getMouseButtonState(MOUSE_LEFT)){        
        m_velocity.setX(1);
    } else {
        m_velocity.setX(0);
    }

    Vector2D * vec = TheInputHandler::Instance()->getMousePosition();
    m_velocity = (*vec - m_position) / 100;
#endif

    if (TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_RIGHT)){
        std::cout << "right is press" << std::endl;
        m_velocity.setX(2);
    }
    if (TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_LEFT)){
        m_velocity.setX(-2);
    }
    if (TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_UP)){
         m_velocity.setY(-2);
    }
    if (TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_DOWN)){
         m_velocity.setY(2);
    }
    handleInput();

    SDLGameObject::update();
#endif
}

void Player::clean(){}

void Player::handleInput(){
    Vector2D * target = TheInputHandler::Instance()
        ->getMousePosition();
    m_velocity = *target - m_position;
    m_velocity /= 50;
}

void Player::ressurect()
{
    TheGame::Instance()->setPlayerLives(TheGame::Instance()->getPlayerLives()-1);
    
    m_position.setX(10);
    m_pisition.setY(200);
    m_bDying = false;

    m_textureID = "player";

    m_currentFrame = 0;
    m_numFrames = 5;
    m_width = 101;
    m_height = 46;

    m_dyingCounter = 0;
    m_invulnerable = true;
}

void Player::handleAnimation(){
    if (m_invulnerable)    
    {
        if (m_invulnerableCounter == m_invulnerableTime)
        {
            m_invulnerable = false;
            m_invulnerableCounter = 0;
            m_alpha = 255;
        }
        else
        {
            if (m_alpha == 255)
            {
                m_alpha = 0;
            }
            else
            {
                m_alpha = 255;
            }
        }

        m_invulnerableCounter++;
    }
    
    if (!m_bDead){
        if (m_velocity.getX() < 0)
        {
            m_angle = -10.0;
        }
        else if(m_velocity.getX() > 0){
            m_angle = 10.0;
        } else { 
            m_angle = 0.0;
        }
    }

    m_currentFrame = int(((SDL_GetTicks())/(100)) % m_numFrames);
}







